<!--  -->
<template>
  <div>
    <div class="second-div">
      <el-row :gutter="40">
        <el-col :span="7">
          <el-card
            class="card-box"
            shadow="hover"
            :body-style="{ padding: '0px' }"
          >
            <img
              src="src\assets\images\insert.png"
              class="image"
              style="border-radius: 50%"
              @click="addCourse()"
              title="添加"
            />
            <div style="padding: 14px">
              <span>课程名</span>
              <div class="bottom">
                <span>老师</span>
                <el-button type="text" class="button" @click="addCourse()"
                  >添加</el-button
                >
              </div>
            </div>
          </el-card>
        </el-col>

        <el-col :span="7" v-for="(item, index) in courses" :key="index">
          <el-card
            class="card-box"
            shadow="hover"
            :body-style="{ padding: '0px' }"
            @click="detailCourse(index)"
          >
            <img
              :src="item.image"
              class="image"
              :alt="item.title"
              title="查看详情"
              @click="detailCourse(index)"
            />
            <div style="padding: 14px">
              <span class="cml-span" :title="item.title">{{ item.title }}</span
              ><br />
              <span class="cml-span" :title="item.intro">{{ item.intro }}</span>
              <div class="bottom clearfix">
                <span>{{ item.createTime }}</span>
                <a
                  class="button el-icon-delete"
                  title="删除"
                  @click.prevent="deleteCourse(index)"
                ></a>

                <a
                  class="button el-icon-edit"
                  title="编辑"
                  @click.prevent="updateCourse(index)"
                ></a>
                <!-- <el-button
                  type="text"
                  class="button"
                  @click="updateCourse(index)"
                  >修改</el-button
                > -->
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
// import course from 'course'
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  inject: ["reload"],
  data() {
    //这里存放数据
    return {
      index: 0,
      addcourse: { name: "课程名称", url: "#", teacher: "教师名字" },
      courses: [
        // {
        //   title: "你还未添加课程",
        //   image: "",
        //   id: "",
        //   intro: "请添加课程",
        //   state: "",
        //   createTime: "",
        // },
      ],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    //获取该老师所有课程
    getTeacherCourse() {
      this.courses = [];
      var id = this.$store.state.user.id;
      var current = 1;
      var size = 100;
      this.$http({
        url: this.$http.adornUrl(
          `/edu-course/selectCourse/${id}/${current}/${size}`
        ), //http://localhost:8082/eduservice/edu-teacher/teacher/20205532
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data) {
          var el = data.data.coursePage.records;
          el.forEach((el, index) => {
            // this.$set(this.group[i], "disable", "false");
            this.courses.push({
              title: el.title,
              image: el.image,
              id: el.id,
              intro: el.intro,
              state: el.state,
              createTime: el.createTime.split(" ")[0],
            });
          });
        } else {
          this.$message.error("数据获取失败");
        }
      });
    },
    //添加课程
    addCourse() {
      this.$router.push({ name: "addcourse", query: { id: 1 } });
      this.reload();
    },
    //课程详细
    detailCourse(index) {
      // this.$router.push({
      //   name: "teacherclass",
      //   query: { courseid: this.courses[index].id, isbav: 0 },
      // });
      let newUrl = this.$router.resolve({
        name: "teacherclass",
        query: { courseid: this.courses[index].id,courseName: this.courses[index].title, isbav: 0 },
      });
      window.open(newUrl.href, "_blank");
    },
    //课程名称和封面修改
    updateCourse(index) {
      this.$router.push({
        name: "addcourse",
        query: {
          id: this.courses[index].id,
          image: this.courses[index].image,
          title: this.courses[index].title,
          intro: this.courses[index].intro,
          pId: this.courses[index].pId,
        },
      });
      this.reload();
    },

    //删除课程
    deleteCourse(index) {
      var id = this.courses[index].id;
      this.$confirm("此操作将永久删除该该课程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl(`/edu-course/deleteCourseById/${id}`), //http://localhost:8082/eduservice/edu-teacher/teacher/20205532
            method: "delete",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 20000) {
              console.log("********" + id);
              this.$message({
                message: "删除课程成功",
                type: "success",
              });
              this.courses.splice(index, 1);
            } else {
              this.$message.error("未能成功删除该课程");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getTeacherCourse();
    //  this.reload();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    // location.reload();
  }, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>
/* @import url(); 引入公共css类 */
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
  margin-left: 10px;
}

.image {
  width: 100%;
  height: 167px;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
.second-div {
  /* border: red 1px solid; */
  width: 73%;
  margin: 0 auto;
}
.card-box {
  margin-bottom: 25px;
}
.cml-span {
  margin-top: 1px;
  overflow: auto;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>